<template>
	<view class="login_div_bg">
		<view class="login_div">
			<view class="login_icon">
				<img src="../../static/icon/login_logo.png" alt="">畅阳题库
			</view>
			<view class="login_title">
				请授权登录
			</view>
			<!-- 			<view class="login_phone">
				12345678911
			</view>
			<view class="login_tip">
				申请使用你的手机号码
			</view> -->
			<!-- <view class="login_wxlogin_btn" @click="appLoginWx">
				微信授权登录
			</view> -->
			<view class="login_wxlogin_btn" v-if="isAss==false" @click="appLoginWx">
				微信授权登录
			</view>
			<button v-else class="login_phonelogin_btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
				绑定手机号
			</button>
		</view>
	</view>
</template>

<script>
	import {
		wxLogin,
		bindinMajor,
		decodeUserInfo,
	} from "@/api/login.js";
	import {
		getMajor,
	} from "@/api/professionalChoice.js";
	import {
		setUserInfo,
		setToken,
		getMajorInfo,
		setMajorInfo
	} from "@/common/util/index.js";
	export default {
		data() {
			return {
				imageUrl: getApp().globalData.imageUrl,
				shape: "",
				code: "",
				isLoading: false,
				location: '',
				majorArr: '',
				phoneisAss: true,
				phisAss: false,
				infos: {},
				unionids: '',
				openids: '',
				sessionKeys: '',
				loginAss: getApp().globalData.loginAss,
				isAss: false
			};
		},
		methods: {
			// 最后一步
			async lastStep(e) {
				// decodeUserInfo({}).then((res)=>{

				// })
				var appId = uni.getAccountInfoSync().miniProgram.appId
				// let appId = 'wxa1a230d527c944e7'
				const res = await decodeUserInfo({
					encryptedData: e.encryptedData,
					iv: e.iv,
					openid: this.openid,
					sessionKey: this.sessionKey,
					appId: appId,
					unionid: this.unionid,
					location: this.location,
					shareId: this.shareUrl ? this.shareUrl.substring(this.shareUrl.indexOf('=') + 1, this
						.shareUrl.indexOf('&')) : ''
				});
				setToken(res.result.token);
				setUserInfo(res.result.userInfo);
				// const valdsa = bindinMajor({
				// 	majorId: getMajorInfo().id
				// });
				// let shareUrl = ''
				// try {
				// 	shareUrl = uni.getStorageSync('shareUrl')
				// } catch (err) {}
				// if (shareUrl) {
				// 	uni.reLaunch({
				// 		url: shareUrl
				// 	})
				// } else {
				// 	uni.navigateTo({
				// 		url: "/pagesA/setUpMajor/setUpMajor",
				// 	});
				// }
				this.$emit('guanbi', false)
				var page = getCurrentPages().pop(); //当前页面
				console.log(page)
				uni.reLaunch({
					url: `${page.$page.fullPath}`
				})
			},
			// 第一步
			// async login(code, e) {
			//   const val = await wxLogin({
			//     code: code,
			//     type: "1",
			//   });
			//   this.openid = val.result.openid;
			//   this.sessionKey = val.result.session_key;
			//   //   2传给后端拿到用户的加密信息
			//   this.lastStep(e);
			// },
			// 登录
			onPone() {},
			getPhoneNumber(e) {
				this.$emit('guanbi', false)
				console.log(e)
				console.log(123)
				// const that = this;
				if (e.detail.errMsg === "getPhoneNumber:ok") {
					uni.login({
						provider: "weixin",
						success: (res) => {
							console.log(e)
							const a = JSON.parse(this.infos);
							console.log(this.unionids)
							this.username = a.nickName;
							this.userImg = a.avatarUrl;
							this.sessionKey = this.sessionKeys;
							this.openid = this.openids;
							this.unionid = this.unionids;
							this.lastStep(e.detail);

						},
						fail: () => {
							uni.showToast({
								title: "微信登录授权失败",
								icon: "none",
							});
						},
					});
				} else {
					// console.log('errrrrr');
					uni.showToast({
						title: "获取手机授权失败",
						icon: "none",
					});
				}
			},
			// return_last_page() {
			// 	uni.navigateBack()
			// },
			// 授权
			async appLoginWx() {
				this.$emit('guanbi', false)
				const that = this;
				if (this.isLoading == false) {
					this.isLoading = true
					uni.login({
						provider: "weixin",
						success: (res2) => {
							console.log(res2)
							that.code = res2.code;
							// const valdsa = bindinMajor({
							// 	majorId: getMajorInfo().id
							// });
						},
						fail: () => {
							console.log('77');
							uni.showToast({
								title: "微信登录授权失败",
								icon: "none",
							});
							that.isLoading = false
						},
					});
					uni.getUserProfile({
						desc: "正在获取", //不写不弹提示框
						success: function(res) {
							// 我的页面头像和名字在这里保存的，手机号授权页面的请求也会用到微信名和头像
							console.log(res, "正在获取");
							// debugger
							that.login(res);

						},
						fail: function(err) {
							console.log('78');
							uni.showToast({
								title: "微信登录授权失败",
								icon: "none",
							});
							uni.showTabBar()
							that.isLoading = false
						},
					});
				}

			},


			async login(data) {

				var appId = uni.getAccountInfoSync().miniProgram.appId
				// let appId = 'wxa1a230d527c944e7'
				console.log(appId, 852)
				console.log(data, 852)
				let reqdata = {
					code: this.code,
					type: "1",
					location: this.location,
					appId: appId,
					nickName: data.userInfo.nickName,
					avatarUrl: data.userInfo.avatarUrl,
				}
				console.log('登录请求数据：', reqdata);
				const val = await wxLogin(reqdata);
				console.log(val, '登录')
				uni.removeStorageSync('MajorInfo')


				if (val.result.token) {
					setToken(val.result.token);
					setUserInfo(val.result.userInfo);
					if (val.result.userInfo) {
						if (val.result.userInfo.majorId) {
							const res = await getMajor();
							res.result.forEach((rest) => {
								rest.wdResourcesMajorList.forEach((item) => {
									if (item.id == val.result.userInfo.majorId) {
										console.log(item, '专业')
										setMajorInfo(item)
									}
								})
							})
						}
					}
					if (getMajorInfo().id) {
						uni.showTabBar()
						console.log(1)
						let shareUrl = ''
						try {
							shareUrl = uni.getStorageSync('shareUrl')
						} catch (err) {}
						if (shareUrl) {
							console.log(shareUrl)
							uni.reLaunch({
								url: shareUrl
							})
						} else {
							uni.removeStorageSync('MajorInfo')
							var page = getCurrentPages().pop(); //当前页面
							console.log(page)
							uni.reLaunch({
								url: `${page.$page.fullPath}`
							})
							console.log(page.$page.fullPath)
						}

						// uni.navigateBack()
					} else {
						let shareUrl = ''
						try {
							shareUrl = uni.getStorageSync('shareUrl')
						} catch (err) {}
						if (shareUrl) {
							console.log(123)
							uni.reLaunch({
								url: shareUrl
							})
						} else {
							uni.navigateTo({
								url: "/pages/setmajor/demo",
							});
						}

					}
				} else {
					this.$emit('guanbi', true)
					this.isAss = true
					this.unionids = val.result.unionid
					this.openids = val.result.openid
					this.sessionKeys = val.result.session_key
					this.infos = JSON.stringify(data.userInfo)
					console.log('手機哈')
					console.log(this.phisAss)
					// uni.navigateTo({
					// 	url: `/pages/mobilePhone/mobilePhone?unionid=${val.result.unionid}&openid=${
					// 	val.result.openid
					//   }&sessionKey=${val.result.session_key}&info=${JSON.stringify(
					// 	data.userInfo
					//   )}`,
					// });
				}
				this.isLoading = false
			},
		},
	}
</script>


<style lang="scss" scoped>
	// page {
	// 	width: 100%;
	// 	height: 100%;
	// 	background: rgba(0, 0, 0, 0.4);
	// }

	.login_wxlogin_btn {
		margin: 70rpx auto 0 auto;
		width: 500rpx;
		height: 80rpx;
		background: #0191A0;
		border-radius: 10rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
		text-align: center;
		line-height: 80rpx;
	}

	.login_phonelogin_btn {
		margin: 20rpx auto 0 auto;
		width: 501rpx;
		height: 81rpx;
		border: 1rpx solid #0191A0;
		background: #0191A0;
		border-radius: 10rpx;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #FFFFFF;
		text-align: center;
		line-height: 81rpx;
	}

	.login_tip {
		margin-top: 24rpx;
		text-align: center;
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #B2B9BA;
	}

	.login_phone {
		text-align: center;
		font-size: 32rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #0191A0;
	}

	.login_title {
		font-size: 36rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #132B2E;
		text-align: center;
		margin-top: 70rpx;
		margin-bottom: 60rpx;
	}

	.login_icon {
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: bold;
		color: #132B2E;

		img {
			width: 46rpx;
			height: 46rpx;
			display: inline-block;
			margin-right: 11rpx;
		}

	}

	// .login_div_bg {
	// 	position: fixed;
	// 	top: 0;
	// 	left: 0;
	// 	height: 100vh;
	// 	width: 100vw;
	// 	background-color: #969696;
	// }

	// .login_div {
	// 	padding: 40rpx 0;
	// 	width: 640rpx;
	// 	background-color: white;
	// 	position: absolute;
	// 	top: 50%;
	// 	left: 50%;
	// 	transform: translate(-50%, -50%);
	// }
	page {
		width: 100%;
		height: 100% !important;
		// background: rgba(0, 0, 0, 0.4);
	}

	.login_div_bg {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		background-color: rgba(0, 0, 0, 0.4);
		position: relative;
		z-index: 999999;
	}

	.login_div {
		width: 600rpx;
		height: 412rpx;
		border-radius: 20rpx;
		background-color: white;
		position: absolute;
		padding-top: 37rpx;
		top: 50%;
		margin-top: -206rpx;
		z-index: 999999;
	}
</style>
